package io.renren.modules.app.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.TDeclareEntity;
import io.renren.modules.app.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-04-24 10:43:02
 */
@RestController
@RequestMapping("app/tdeclare")
public class TDeclareController {
    @Autowired
    private TDeclareService tDeclareService;
    @Autowired
    private TProductService tProductService;
    @Autowired
    private TCompanyService tCompanyService;
    @Autowired
    private TInsuranceService tInsuranceService;
    @Autowired
    private TOrganizationService tOrganizationService;



    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("app:tdeclare:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tDeclareService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("app:tdeclare:info")
    public R info(@PathVariable("id") Long id){
        TDeclareEntity tDeclare = tDeclareService.getById(id);

        return R.ok().put("tDeclare", tDeclare);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("app:tdeclare:save")
    public R save(@RequestBody TDeclareEntity tDeclare){
        tDeclareService.save(tDeclare);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("app:tdeclare:update")
    public R update(@RequestBody TDeclareEntity tDeclare){
        ValidatorUtils.validateEntity(tDeclare);
        tDeclareService.updateById(tDeclare);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("app:tdeclare:delete")
    public R delete(@RequestBody Long[] ids){
        tDeclareService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



    /**
     * 提报,信息保存。
     */
    @RequestMapping("/submitDeclare")
    public R submitDeclare(TDeclareEntity tDeclare){
        try {
            Integer companyId = tDeclare.getCompanyId();
            if(companyId==null){
                return R.error("未登录，请先登录！");
            }
            Integer declareTypeId = tDeclare.getDeclareTypeId();
            if(declareTypeId==null){
                return R.error("数据不完整，请联系管理员！");
            }
            tDeclareService.submit(tDeclare);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }






}
